package com.ceres.farm.controller;

import com.ceres.entity.Result;
import com.ceres.entity.StatusCode;
import com.ceres.farm.pojo.CustFarmlandCrops;
import com.ceres.farm.service.CustFarmlandCropsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("/custCrops")
public class CustFarmlandCropsController {
    @Autowired
    CustFarmlandCropsService custFarmlandCropsService;

    /**
     * 农作物种植
     * @param custFarmlandCrops
     * @return
     */
    @PostMapping("/add")
    public Result addCrops(@RequestBody CustFarmlandCrops custFarmlandCrops){

        try {
            custFarmlandCropsService.addCrops(custFarmlandCrops);
            return new Result(true, StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 农作物详情
     */

    @GetMapping("/cropsInfo/{id}")
    public Result cropsInfo(@PathVariable Integer id){
        try {
            Map<String,Object> map= custFarmlandCropsService.cropsInfo(id);
            return new Result(true, StatusCode.OK,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }

    /**
     * 农作物状态修改
     */
    @PutMapping("/updateStage/{stage}/{fid}")
    public Result updateStage(@PathVariable String stage,@PathVariable Integer fid){

        try {
            custFarmlandCropsService.updateStage(stage,fid);
            return new Result(true, StatusCode.OK);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR);
        }
    }
}
